#!/bin/bash

#readme 
#Aaron 461177348@qq.com 20181119
#install Advanced installer for msi build.
#install subversion for windows.

#ecco_error msg
function echo_error()
{
	err_msg=$1
    echo -e "\033[31m $err_msg \033[0m"
}

#echo_info msg
function echo_info()
{
	err_msg=$1
    echo -e "\033[33m $err_msg \033[0m"
}

CPU_CORES=4

#build_project Project_name Project_path
function build_project()
{
	Project_name=$1
	Project_path=$2

	echo_info "编译$Project_name"
	cd $Project_path
	cur_dir=`pwd`
	echo  "cd $cur_dir."
	qmake.exe $Project_name.pro -r -spec win32-g++ "CONFIG+=release" && C:/msys64/mingw64/bin/mingw32-make.exe qmake_all >> $Project_name.qmake_log
		if [ $? != 0 ]
		then
		       	echo_error "qmake $Project_name.pro failed!!! Detail in $Project_name.qmake_log"
			exit 1
		fi
	echo  "qmake $Project_name.pro success."

	mingw32-make -release -j$CPU_CORES  >> $Project_name.make_log
		if [ $? != 0 ]
		then
		       	echo_error  "Do $Project_name mingw32-make -release -j$CPU_CORES failed!!!  Detail in $Project_name.make_log"
			exit 1
		fi
	echo  "make $Project_name.pro success."
}

if [ $# -ne 2 ]
	then
		echo "Usage: ./autobuild_ly svn_name svn_pwd"
		exit 1
fi

echo_info "准备数据"
USERNAME=$1
PASSWORD=$2

SOURCE_URL="https://10.88.1.11/svn/wtgeo/WtgeoProducts/"
TAG_URL="https://10.88.1.11/svn/wtgeo/Tag/WtgeoProducts/"
RELEASE_URL="https://10.88.1.11/svn/wtgeo/release/WtgeoProducts/"

SHARE_USER_NAME="share"
SHARE_USER_PWD="Ss123456"

echo_info "获取发布版本号"
svn list $TAG_URL --username=$USERNAME --password=$PASSWORD > svnlist
line=`cat svnlist|wc -l`
a=${line}"p"
temp1=$(sed -n $a ${Project}svnlist) 
read -r -a  words <<< "$temp1"
IFS=_
read -r -a ff <<< "${words[0]}"
Dvar=`date +%Y-%m-%d`
IFS=
NUMBER=`expr ${ff[1]} + 1`
NUMBER=$(printf '%d' $NUMBER)
rm svnlist

result=`svn update`
svn_reversion=`echo $result | tr -cd "[0-9]"`

VERSION="V3.0.$NUMBER.$svn_reversion"
PRODUCT_VERSION="3.0.$NUMBER.$svn_reversion"
FILE_VERSION=3,0,$NUMBER,$svn_reversion
LABEL="build_"$NUMBER"_"${Dvar}
echo $VERSION
echo $LABEL



# echo "定义发布版本目录"
WMPS_DIR="wmps"
DPS="DPS"
DIS="DIS"
RMS="RMS"
PCS="PCS"
SERVER_DIR="Server"
DATA_ACQUIRE="DataAcquire"
HTTP_SERVER="HttpServer"
WMPS_CMD_TOOL="WmpsCmdTool"
RESOURCE_DIR="Resource"
CONFIG_DIR="config"
IMAGES_DIR="images"
CONFIGURATION_DIR="Configuration"
TIMEGRID_DIR="timegrid"


cd ../..
cur_dir=`pwd`
echo  "cd $cur_dir."

echo_info "签出代码"
if [ ! -d "WtgeoProducts" ]
then
#svn checkout $SOURCE_URL --username=$USERNAME --password=$PASSWORD
	if [ $? != 0 ]
	then 
		echo_error "Checkout code failed!!!" 
		exit 1
	fi
else
	cd WtgeoProducts
	svn update
	cd .. 
fi


echo_info "修改version.h"
cd WtgeoProducts/System/WMPS/Common/WmpsInc
VDATE=`date +%Y-%m-%d`
VTIME=`date +%H:%M:%S`
echo "alter version.h file"
if [ -f "./version.h" ]
then 
	rm -rf ./version.h
	if [ $? != 0 ]
	then
      echo_error  "rm local version.h error!!!"
	exit 1
	fi
fi
touch version.h
if [ -f "./version.h" ]
then
	echo "//#############################################################################" >> version.h
	echo "//# Generated by autobuild ${VDATE} ${VTIME}" >> version.h
	echo "//# Do not modify this file" >> version.h
	echo "//#############################################################################" >> version.h
	echo "#ifndef __VERSION_H__" >> version.h
	echo -e "#define __VERSION_H__\n" >> version.h

	echo "#define VERSION       \"$VERSION\"" >> version.h
	echo "#define VER_PRODUCTVERSION_STR       \"$PRODUCT_VERSION\"" >> version.h
	echo "#define VER_FILE_VERSION       $FILE_VERSION" >> version.h
	echo -e "#define BUILD_TIME    \"${VDATE} ${VTIME}\"\n" >> version.h

	echo "#endif //__VERSION_H__" >> version.h
else
        echo_error  "create version.h error!!!"
	exit 1
fi

startTime=`date +"%s.%N"`

cd ../../APP/

build_project $DPS $DPS

build_project $DIS ../$DIS

build_project $RMS ../$RMS

build_project $HTTP_SERVER ../$HTTP_SERVER

build_project $DATA_ACQUIRE ../$DATA_ACQUIRE

build_project $WMPS_CMD_TOOL ../$WMPS_CMD_TOOL

build_project $PCS ../$PCS


endTime=`date +"%s.%N"`
echo `awk -v x1="$(echo $endTime | cut -d '.' -f 1)" -v x2="$(echo $startTime | cut -d '.' -f 1)" -v y1="$[$(echo $endTime | cut -d '.' -f 2) / 1000]" -v y2="$[$(echo $startTime | cut -d '.' -f 2) /1000]" 'BEGIN{printf "Build time:%.f s",(x1-x2)+(y1-y2)/1000000}'`

cd ../../../../Build
cur_dir=`pwd`
echo  "cd $cur_dir."

./advanced_installer_build.bat

echo_info "msi build success."

#shutdown /f -s -t 00